package cn.simonfish.carrier.controller.view;

import cn.simonfish.carrier.common.LoginInfo;
import cn.simonfish.carrier.controller.BaseController;
import cn.simonfish.carrier.model.SysUser;
import cn.simonfish.carrier.model.SysUserSiteConfig;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.jfinal.plugin.activerecord.Db.find;

/**
 * Created by simon on 2017/9/10.
 */
public class UserView extends BaseController {


    public void profile(){
        SysUser user = SysUser.dao.findById(getLoginInfo().getUserId());
        setAttr("user",user);
        renderFreeMarker("template/user/profile.html");
    }

    public void login(){
        setAttr("isLogin",(getLoginInfo()!=null));
        renderFreeMarker("template/user/login.html");
    }

    public void logout(){
        setSessionAttr(LoginInfo.SESSION_LOGIN,null);
        setSessionAttr(LoginInfo.SESSION_LAST_URL,null);
        redirect("/index.html");
    }

    public void register(){
        renderFreeMarker("template/user/register.html");
    }

    public void site_config(){
        //构造用户配置信息
        LoginInfo loginInfo = getLoginInfo();
        //得到所有网站信息
        List<Record> records = find("select REGION,WEBSITE from SETTING_WEBSITE");
        List<Record> sites = new ArrayList<>();
        for (Record record : records){
            if(loginInfo.hasSiteAuth(record.getStr("WEBSITE"))){
                sites.add(record);
            }
        }
        setAttr("sites",sites);

        List<SysUserSiteConfig> configs = SysUserSiteConfig.dao.find("select * from sys_user_site_config where user_phone=?",loginInfo.getPhone());
        Map<String,SysUserSiteConfig> configMap = new HashMap<>();
        for (SysUserSiteConfig conf:configs) {
            String site = conf.getStr("site");
            //过滤未订阅的
            if(loginInfo.hasSiteAuth(site)){
                configMap.put(site,conf);
            }
        }
        setAttr("configMap",configMap);
        renderFreeMarker("template/user/site_config.html");
    }

    @Before(Tx.class)
    public void submit_config(){
        String[] recharge = getParaValues("recharge");
        String[] withdraw = getParaValues("withdraw");
        String[] display = getParaValues("display");
        if(recharge!=null && withdraw!=null && display!=null){
            saveSiteConfig(recharge, withdraw, display);
            setAttr("msg","配置保存成功");
        }

        site_config();
    }

    private void saveSiteConfig(String[] recharge, String[] withdraw, String[] display) {
        LoginInfo loginInfo = getLoginInfo();
        Db.update("delete from sys_user_site_config where user_phone=?",loginInfo.getPhone());
        Map<String,SysUserSiteConfig> configMap = new HashMap<>();
        //构造充值
        for (String site:recharge) {
            SysUserSiteConfig config =configMap.get(site);
            if(config ==null){
                config = new SysUserSiteConfig();
                configMap.put(site,config);
                config.set("site",site);
            }
            config.set("recharge_enable",1);
        }
        for (String site:withdraw) {
            SysUserSiteConfig config =configMap.get(site);
            if(config ==null){
                config = new SysUserSiteConfig();
                configMap.put(site,config);
                config.set("site",site);
            }
            config.set("withdraw_enable",1);
        }
        for (String site:display) {
            SysUserSiteConfig config =configMap.get(site);
            if(config ==null){
                config = new SysUserSiteConfig();
                configMap.put(site,config);
                config.set("site",site);
            }
            config.set("display_enable",1);
        }
        //保存
        for (SysUserSiteConfig config:configMap.values()) {
            config.set("user_phone",loginInfo.getPhone());
            config.save();
        }
        //初始化网站配置
        List<SysUserSiteConfig> configs = SysUserSiteConfig.dao.find("select * from sys_user_site_config s where s.user_phone=? ",loginInfo.getPhone());
        loginInfo.clearConfig();
        for (SysUserSiteConfig conf: configs      ) {
            loginInfo.addConfig(conf);
        }
    }
}
